package cn.huaqingcheng.tool.data;

import lombok.EqualsAndHashCode;
import lombok.Getter;

import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * 简单的分页参数
 *
 * @apiNote 实现 {@link Serializable} 只是以防万一, 并不保证 {@link #list} 内元素可序列化
 **/
@SuppressWarnings("unused")
@Getter
@EqualsAndHashCode
public class SimplePage<T> implements Page<T>, Serializable {

    @Serial
    private static final long serialVersionUID = 1;

    /**
     * 页大小
     */
    @Positive
    private Integer size;

    /**
     * 页码
     */
    @Positive
    private Integer current;

    /**
     * 总记录数
     */
    @PositiveOrZero
    private Integer total;

    /**
     * 可选的, 数据列表
     */
    private List<T> list;

    public SimplePage() {
        this(0, 0, 0, List.of());
    }

    public SimplePage(Integer size, Integer current, Integer total, List<T> list) {
        this.size = Objects.requireNonNullElse(size, 0);
        this.current = Objects.requireNonNullElse(current, 0);
        this.total = Objects.requireNonNullElse(total, 0);
        this.list = Objects.requireNonNullElse(list, List.of());
    }

    public SimplePage(Integer size, Integer current) {
        this(size, current, 0, List.of());
    }

    public SimplePage(Page<T> oldPage) {
        this(oldPage.getSize(), oldPage.getCurrent(), oldPage.getTotal(), oldPage.getList());
    }

    @Override
    public Page<T> setSize(Integer size) {
        this.size = size;
        return this;
    }

    @Override
    public Page<T> setCurrent(Integer current) {
        this.current = current;
        return this;
    }

    @Override
    public Page<T> setTotal(Integer total) {
        this.total = total;
        return this;
    }

    @Override
    public Page<T> setList(List<T> list) {
        this.list = list;
        return this;
    }

}
